/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EMPLOYEE_PROFILE2.java
 *
 * Created on Oct 22, 2010, 2:14:20 PM
 */

package Java;

import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author shyan
 */
public class EMPLOYEE_PROFILE2 extends javax.swing.JFrame {

    private String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private String userID = "HR";
    private String userPW = "hr";
    private Connection con;
    private Statement stmt;
    private ResultSet rset;
    String ID="";
    String NAME="";
    String POSITION="";

    /** Creates new form EMPLOYEE_PROFILE2 */
    public EMPLOYEE_PROFILE2()
    {
        super("EMPLOYEE PROFILE");
        initComponents();
        setSize(700,500);
        //ID=id;
        //NAME=name;
        //POSITION=position;
        setLocationRelativeTo(null);
        connectDatabase();
        DisplayData();
    }



    private void connectDatabase()
    {
        try
        {
            Class.forName("oracle.jdbc.OracleDriver");
            con=DriverManager.getConnection(url, userID, userPW);
        }
        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(EMPLOYEE_PROFILE.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EMPLOYEE_PROFILE.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void DisplayData()
    {

        Vector Attributes = new Vector();
        Vector Occurence = new Vector();

        try
        {
            String extractQuery="SELECT EMP_ID AS Employee_ID,EMP_NAME AS Name,"
                    + "EMP_IC AS IC_number,EMP_POSITION AS Position FROM EMPLOYEE ORDER BY EMP_ID";

            stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rset = stmt.executeQuery(extractQuery);
            
            
            rset.first();
            ResultSetMetaData INFO = rset.getMetaData();
            int columns = INFO.getColumnCount();
            System.out.println("Number of column -> "+columns);

            // Get the Attributes (column name)
            for (int i = 1; i <= columns; i++)
            {
                Attributes.addElement(INFO.getColumnName(i));
            }
            rset.first();
            if (rset.first() == true) // USED TO EXTRACT THE 1st ROW DATA
            {
                Vector ROW = new Vector(columns);
                for (int i = 1; i <= columns; i++)
                {
                    ROW.addElement(rset.getObject(i));
                }
                Occurence.addElement(ROW);
            }
            else
            {
                System.out.println("Data Extraction Failed !!!");
                JOptionPane.showMessageDialog(null, "No Record Found !", "MESSAGE!", JOptionPane.INFORMATION_MESSAGE);
            }

            while (rset.next())
            {
               Vector row = new Vector(columns);
               for (int i = 1; i <= columns; i++)
               {
                 row.addElement(rset.getObject(i));
               }
                 Occurence.addElement(row);
            }
           
         con.close();
        }
        catch (SQLException e)
        {
            System.out.println(e);
            System.out.print("sssss");
        }
        DefaultTableModel model = new DefaultTableModel(Occurence, Attributes);
        table.setModel(model);

   }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jSeparator2 = new javax.swing.JSeparator();
        back = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(700, 500));
        setResizable(false);

        jPanel1.setMaximumSize(new java.awt.Dimension(700, 500));
        jPanel1.setMinimumSize(new java.awt.Dimension(700, 500));
        jPanel1.setPreferredSize(new java.awt.Dimension(700, 500));
        jPanel1.setLayout(null);

        jLabel1.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 24));
        jLabel1.setText("EMPLOYEE  PROFILE");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(60, 20, 270, 50);

        table.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 13));
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tableMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(table);
        table.getColumnModel().getColumn(0).setPreferredWidth(15);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(40, 110, 620, 300);
        jPanel1.add(jSeparator2);
        jSeparator2.setBounds(0, 84, 700, 10);

        back.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        back.setText("BACK");
        back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backActionPerformed(evt);
            }
        });
        jPanel1.add(back);
        back.setBounds(560, 430, 100, 30);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/ZzZzZzZ.jpg"))); // NOI18N
        jPanel1.add(jLabel2);
        jLabel2.setBounds(0, 0, 700, 500);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(300, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(200, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMousePressed
        int RowSelected=table.getSelectedRow();
        String number = table.getValueAt(RowSelected, 0).toString();
        System.out.println(number);
        this.setVisible(false);
        new EMPLOYEE_PROFILE(number).setVisible(true);
    }//GEN-LAST:event_tableMousePressed

    private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
        this.setVisible(false);
        new EMPLOYEE_PROFILE().setVisible(true);
    }//GEN-LAST:event_backActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EMPLOYEE_PROFILE2().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton back;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables

}
